package com.cspm.ahjy.bc.office.word.util;

import org.dom4j.Attribute;
import org.dom4j.Element;

import com.cspm.ahjy.bc.office.word.domain.MSBorder;

/**
 * Border工具类
 * @author ServerZhang
 * @date 2017年9月30日
 */
public class BorderUtil {

	/**
	 * 通过等<w:top>、<w:left>标签获取Border对象
	 * @param locationElement
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月30日
	 */
	public static MSBorder getMsBorder(Element locationElement){
		MSBorder msBorder = new MSBorder();
		
		Attribute val = locationElement.attribute("val");
		if (val != null) {
			msBorder.setType(val.getText());
		}
		
		Attribute sz = locationElement.attribute("sz");
		if (sz != null) {
			msBorder.setSize(Integer.parseInt(sz.getText()));
		}
		
		Boolean isShadow = Boolean.FALSE;//当样式为阴影时在计算宽度时除以2
		Attribute shadow = locationElement.attribute("shadow");
		if (shadow != null) {
			String text = shadow.getText();
			if ("on".equals(text)) {
				isShadow = Boolean.TRUE;
				msBorder.setShadow(isShadow);
			}
		}
		
		Attribute bdrwidth = locationElement.attribute("bdrwidth");
		if (bdrwidth != null) {
			int parseInt = Integer.parseInt(bdrwidth.getText());
			if (isShadow) {
				msBorder.setWidth(parseInt/2);
			}else {
				msBorder.setWidth(parseInt);
			}
		}
		
		Attribute space = locationElement.attribute("space");
		if (space != null) {
			msBorder.setSpace(Integer.parseInt(space.getText()));
		}
		
		Attribute color = locationElement.attribute("color");
		if (color != null) {
			msBorder.setColor(color.getText());
		}
		
		return msBorder;
	}
}
